﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SqlDataAccess;

namespace QuanLyBanHang
{
    public partial class F910_Bao_cao_so_du_tai_mot_thoi_diem : Form
    {
        public F910_Bao_cao_so_du_tai_mot_thoi_diem()
        {
            InitializeComponent();
			set_define_events();

		}

		#region Members
		BKIQuanLyBanHangEntities db = new BKIQuanLyBanHangEntities();
		#endregion

		#region Private Methods
		private void set_define_events()
		{
			this.Load += F910_Bao_cao_so_du_tai_mot_thoi_diem_Load;
			this.m_cmd_xem_bao_cao.Click += m_cmd_xem_bao_cao_Click;
            this.m_txt_loc_du_lieu.TextChanged += m_txt_loc_du_lieu_TextChanged;
		}

		private void load_data_to_grid()
		{
			var v_lst_so_du = db.GD_SO_DU
								.Where(x => (x.DM_SAN_PHAM.TEN_SAN_PHAM.Contains(m_txt_loc_du_lieu.Text)
											|| m_txt_loc_du_lieu.Text.Equals(""))
											&&x.NGAY_THANG<=m_dtp_ngay_bao_cao.Value)
								.GroupBy(x => x.ID_SAN_PHAM)
								.Select(x => x.OrderByDescending(y => y.NGAY_THANG).FirstOrDefault())
								.OrderByDescending(x => x.NGAY_THANG)   
								.Select(x => new
								{
									ID = x.ID,
									TEN_SAN_PHAM = x.DM_SAN_PHAM.TEN_SAN_PHAM,
									NGAY_THANG = x.NGAY_THANG,
									SO_LUONG = x.SO_LUONG,
									DON_VI_TINH = x.DM_SAN_PHAM.DON_VI_TINH
								})

								.ToList();
			m_grv.DataSource = v_lst_so_du;
			m_grv.Columns[0].Visible = false;
			m_grv.Columns[1].HeaderText = "Sản phẩm";
			m_grv.Columns[1].Width = 200;
			m_grv.Columns[2].HeaderText = "Ngày nhập";
			m_grv.Columns[2].Width = 100;
			m_grv.Columns[3].HeaderText = "Số lượng";
			m_grv.Columns[4].HeaderText = "Đơn vị tính";
			m_grv.Columns[4].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
		}
		#endregion

		#region Events
		void m_cmd_xem_bao_cao_Click(object sender, EventArgs e)
		{
			try
			{
				load_data_to_grid();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

		void F910_Bao_cao_so_du_tai_mot_thoi_diem_Load(object sender, EventArgs e)
		{
			try
			{
				load_data_to_grid();
			}
			catch (Exception v_e)
			{
				MessageBox.Show(v_e.ToString());
			}
		}

        void m_txt_loc_du_lieu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                load_data_to_grid();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }

        private void m_cmd_quay_lai_Click(object sender, EventArgs e)
        {
            try
            {
                this.Close();
            }
            catch (Exception v_e)
            {
                MessageBox.Show(v_e.ToString());
            }
        }
		#endregion

	}
}
